<?
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가

// 게시판에서 두단어 이상 검색 후 검색된 게시물에 코멘트를 남기면 나오던 오류 수정
$sop = strtolower($sop);
if ($sop != "and" && $sop != "or")
    $sop = "and";

function get_view_new($write_row, $board, $skin_path, $subject_len=125)
{
	static $g4, $config;
	global $qstr, $page;

	//$t = get_microtime();
	
	// 배열전체를 복사
	$list = $write_row;
	unset($write_row);

	//	$list['is_notice'] = preg_match("/[^0-9]{0,1}{$list['wr_id']}[\r]{0,1}/",$board['bo_notice']);
	//제목
	if ($subject_len)
		$list['subject'] = conv_subject($list['ntc_title'], $subject_len, "…");
	else
		$list['subject'] = conv_subject($list['ntc_title'], $board['bo_subject_len'], "…");
	
	// 목록에서 내용 미리보기 사용한 게시판만 내용을 변환함 (속도 향상) : kkal3(커피)님께서 알려주셨습니다.
	if ($board['bo_use_list_content'])
	{
		$html = 0;
		// 		if (strstr($list['wr_option'], "html1"))
			// 			$html = 1;
			// 		else if (strstr($list['wr_option'], "html2"))
				// 			$html = 2;

			$list['content'] = conv_content($list['ntc_content'], $html);
	}
	//$list['content'] = $list['ntc_content'];
	// 	$list['comment_cnt'] = "";
	// 	if ($list['wr_comment'])
		// 		$list['comment_cnt'] = "($list[wr_comment])";

	// 당일인 경우 시간으로 표시함
	$list['datetime'] = substr($list['reg_date'],0,10);
	$list['datetime2'] = $list['reg_date'];
	if ($list['datetime'] == $g4['time_ymd'])
		$list['datetime2'] = substr($list['datetime2'],11,5);
	else
		$list['datetime2'] = substr($list['datetime2'],5,5);
	// 4.1
	// 	$list['last'] = substr($list['wr_last'],0,10);
	// 	$list['last2'] = $list['wr_last'];
	// 	if ($list['last'] == $g4['time_ymd'])
		// 		$list['last2'] = substr($list['last2'],11,5);
		// 	else
			// 		$list['last2'] = substr($list['last2'],5,5);

	// 	$list['wr_homepage'] = get_text(addslashes($list['wr_homepage']));

	$tmp_name = get_text(cut_str($list['regr_nm'], $config['cf_cut_name'])); // 설정된 자리수 만큼만 이름 출력
// 	if ($board['bo_use_sideview'])
// 		$list['name'] = get_sideview($list['mb_id'], $tmp_name, $list['wr_email'], $list['wr_homepage']);
// 	else
// 		$list['name'] = "<span class='".($list['mb_id']?'member':'guest')."'>$tmp_name</span>";
	
	// 	$reply = $list['wr_reply'];
	$list['name'] = $list['regr_nm'];
	$list['reply'] = "";
	if (strlen($reply) > 0)
	{
		for ($k=0; $k<strlen($reply); $k++)
			$list['reply'] .= ' &nbsp;&nbsp; ';
	}

	//     $list['icon_reply'] = "";
	//     if ($list['reply'])
		//     		$list['icon_reply'] = "<img src='$skin_path/img/icon_reply.gif' align='absmiddle'>";

		//     $list['icon_link'] = "";
		//     if ($list['wr_link1'] || $list['wr_link2'])
			//     	$list['icon_link'] = "<img src='$skin_path/img/icon_link.gif' align='absmiddle'>";

		// 분류명 링크
		$list['ca_name_href'] = "/adm/adm_new/board.php?bo_table=$board[bo_table]&sca=".urlencode($list['ca_name']);

		$list['href'] = "/adm/adm_new/board.php?bo_table=$board[bo_table]&wr_id=$list[id]" . $qstr;

		if ($board['bo_use_comment'])
			$list['comment_href'] = "javascript:win_comment('/adm/adm_new/board.php?bo_table=$board[bo_table]&wr_id=$list[id]&cwin=1');";
		else
			$list['comment_href'] = $list['href'];

		$list['icon_new'] = "";
		if ($list['wr_datetime'] >= date("Y-m-d H:i:s", $g4['server_time'] - ($board['bo_new'] * 3600)))
			$list['icon_new'] = "<img src='$skin_path/img/icon_new.gif' align='absmiddle'>";

		//     $list['icon_hot'] = "";
		//     if ($list['wr_hit'] >= $board['bo_hot'])
			//     	$list['icon_hot'] = "<img src='$skin_path/img/icon_hot.gif' align='absmiddle'>";

			//     $list['icon_secret'] = "";
			//     if (strstr($list['wr_option'], "secret"))
				//     	$list['icon_secret'] = "<img src='$skin_path/img/icon_secret.gif' align='absmiddle'>";

			// 링크
			for ($i=1; $i<=$g4['link_count']; $i++)
			{
			$list['link'][$i] = set_http(get_text($list["wr_link{$i}"]));
			$list['link_href'][$i] = "/adm/adm_new/link.php?bo_table=$board[bo_table]&wr_id=$list[id]&no=$i" . $qstr;
			$list['link_hit'][$i] = (int)$list["wr_link{$i}_hit"];
			}

			// 가변 파일
			// 	$list['file'] = get_file($board['bo_table'], $list['wr_id']);

			// 	if ($list['file']['count'])
				//         $list['icon_file'] = "<img src='$skin_path/img/icon_file.gif' align='absmiddle'>";


			


			return $list;
}

//include_once("$board_skin_path/view.head.skin.php");

$sql_search = "";
// 검색이면
if ($sca || $stx) {
    // where 문을 얻음
    $sql_search = get_sql_search($sca, $sfl, $stx, $sop);
    $search_href = "./board.php?bo_table=$bo_table&page=$page" . $qstr;
    $list_href = "./board.php?bo_table=$bo_table";
} else {
    $search_href = "";
    $list_href = "./board.php?bo_table=$bo_table&page=$page";
}

if (!$board[bo_use_list_view]) {
    if ($sql_search)
        $sql_search = " and " . $sql_search;

    // 윗글을 얻음
    //$sql = " select wr_id, wr_subject from $write_table where wr_is_comment = 0 and wr_num = '$write[wr_num]' and wr_reply < '$write[wr_reply]' $sql_search order by wr_num desc, wr_reply desc limit 1 ";
    $sql = " select id, ntc_title from $write_table order by id desc limit 1 ";
    $prev = sql_fetch($sql);
    // 위의 쿼리문으로 값을 얻지 못했다면
    if (!$prev[id])     {
        //$sql = " select wr_id, wr_subject from $write_table where wr_is_comment = 0 and wr_num < '$write[wr_num]' $sql_search order by wr_num desc, wr_reply desc limit 1 ";
    	$sql = " select id, ntc_title from $write_table order by id desc limit 1 ";
        $prev = sql_fetch($sql);
    }

    // 아래글을 얻음
    //$sql = " select wr_id, wr_subject from $write_table where wr_is_comment = 0 and wr_num = '$write[wr_num]' and wr_reply > '$write[wr_reply]' $sql_search order by wr_num, wr_reply limit 1 ";
    $sql = " select id, ntc_title from $write_table order by id limit 1 ";

    $next = sql_fetch($sql);
    // 위의 쿼리문으로 값을 얻지 못했다면
    if (!$next[id]) {
        //$sql = " select wr_id, wr_subject from $write_table where wr_is_comment = 0 and wr_num > '$write[wr_num]' $sql_search order by wr_num, wr_reply limit 1 ";
    	$sql = " select id, ntc_title from $write_table order by id limit 1 ";
        $next = sql_fetch($sql);
    }
}

// 이전글 링크
$prev_href = "";
if ($prev[id]) {
    $prev_wr_subject = get_text(cut_str($prev[ntc_title], 255));
    $prev_href = "./board.php?bo_table=$bo_table&wr_id=$prev[wr_id]&page=$page" . $qstr;
}

// 다음글 링크
$next_href = "";
if ($next[id]) {
    $next_wr_subject = get_text(cut_str($next[ntc_title], 255));
    $next_href = "./board.php?bo_table=$bo_table&wr_id=$next[wr_id]&page=$page" . $qstr;
}

// 쓰기 링크
$write_href = "";
if ($member[mb_level] >= $board[bo_write_level])
    $write_href = "./write.php?bo_table=$bo_table";

// 답변 링크
$reply_href = "";
if ($member[mb_level] >= $board[bo_reply_level])
    $reply_href = "./write.php?w=r&bo_table=$bo_table&wr_id=$wr_id" . $qstr;

// 수정, 삭제 링크
$update_href = $delete_href = "";
// 로그인중이고 자신의 글이라면 또는 관리자라면 패스워드를 묻지 않고 바로 수정, 삭제 가능
if (($member[mb_id] && ($member[mb_id] == $write[mb_id])) || $is_admin) {
    $update_href = "./write.php?w=u&bo_table=$bo_table&wr_id=$wr_id&page=$page" . $qstr;
    $delete_href = "javascript:del('./delete.php?bo_table=$bo_table&wr_id=$wr_id&page=$page".urldecode($qstr)."');";
    if ($is_admin)
    {
        set_session("ss_delete_token", $token = uniqid(time()));
        $delete_href = "javascript:del('./delete.php?bo_table=$bo_table&wr_id=$wr_id&token=$token&page=$page".urldecode($qstr)."');";
    }
}
else if (!$write[mb_id]) { // 회원이 쓴 글이 아니라면
    $update_href = "./password.php?w=u&bo_table=$bo_table&wr_id=$wr_id&page=$page" . $qstr;
    $delete_href = "./password.php?w=d&bo_table=$bo_table&wr_id=$wr_id&page=$page" . $qstr;
}

// 최고, 그룹관리자라면 글 복사, 이동 가능
$copy_href = $move_href = "";
if ($write[wr_reply] == "" && ($is_admin == "super" || $is_admin == "group")) {
    $copy_href = "javascript:win_open('./move.php?sw=copy&bo_table=$bo_table&wr_id=$wr_id&page=$page".$qstr."', 'boardcopy', 'left=50, top=50, width=500, height=550, scrollbars=1');";
    $move_href = "javascript:win_open('./move.php?sw=move&bo_table=$bo_table&wr_id=$wr_id&page=$page".$qstr."', 'boardmove', 'left=50, top=50, width=500, height=550, scrollbars=1');";
}

$scrap_href = "";
$good_href = "";
$nogood_href = "";
if ($member[mb_id]) {
    // 스크랩 링크
    $scrap_href = "./scrap_popin.php?bo_table=$bo_table&wr_id=$wr_id";

    // 추천 링크
    if ($board[bo_use_good])
        $good_href = "./good.php?bo_table=$bo_table&wr_id=$wr_id&good=good";

    // 비추천 링크
    if ($board[bo_use_nogood])
        $nogood_href = "./good.php?bo_table=$bo_table&wr_id=$wr_id&good=nogood";
}

$write = sql_fetch(" select * from $write_table where id = '$wr_id' ");


$view = get_view_new($write, $board, $board_skin_path, 255);

$ls = array();
function get_comm_new($write_row)
{
	//$t = get_microtime();

	// 배열전체를 복사
	
	$ls = $write_row;
	unset($write_row);
	
	$ls['ntc'] = $write_row['ntc_id'];
	$ls['mb'] = $write_row['mb_id'];
	
	return $ls;
}
//조회 이력 
$list=array();

$sql = "select a.id, a.ntc_id, b.mb_1, a.mb_id, a.view_date from notice_view_history a LEFT OUTER JOIN g4_member b ON a.mb_id = b.mb_id where ntc_id = '$wr_id' group by mb_id order by view_date desc";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
	$list[$i] = $row;
	if( $list[$i][mb_id] == "sv" ){
		$list[$i][mb_1] = "관리자";
	}
}
if (strstr($sfl, "subject")){
    $view[subject] = search_font($stx, $view[subject]);
}

// $html = 0;
// if (strstr($view[wr_option], "html1"))
//     $html = 1;
// else if (strstr($view[wr_option], "html2"))
//     $html = 2;

$view[content] = conv_content($view[wr_content], $html);

if (strstr($sfl, "content"))
    $view[content] = search_font($stx, $view[content]);
/*--- 2013.12.13 notice게시판 동영상 플레이 문제때문에 임시 주석함. ---*/
//$view[content] = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' onclick='image_window(this)' style='cursor:pointer;' \\2 \\3", $view[content]);

//$view[rich_content] = preg_replace("/{img\:([0-9]+)[:]?([^}]*)}/ie", "view_image(\$view, '\\1', '\\2')", $view[content]);
//$view[rich_content] = preg_replace("/{이미지\:([0-9]+)[:]?([^}]*)}/ie", "view_image(\$view, '\\1', '\\2')", $view[content]);


$is_signature = false;
$signature = "";
if ($board[bo_use_signature] && $view[mb_id])
{
    $is_signature = true;
    $mb = get_member($view[mb_id]);
    $signature = $mb[mb_signature];

    //$signature = bad_tag_convert($signature);
    // 081022 : CSRF 보안 결함으로 인한 코드 수정
    $signature = conv_content($signature, 1);
}

echo "<script type='text/javascript' src='{$g4['path']}/js/ajax.js'></script>";
include_once("./view.skin.php");

//include_once("$board_skin_path/view.tail.skin.php");
?>
